package com.xpec.cipher.tals.record;

import com.xpec.cipher.tals.TalsConstance;
import com.xpec.cipher.tals.Util;

/**
 * Client用來送出加密過的 "秘密前体" 給 Server
 * @author linyuntai
 */
public class RSAEncryptedPreMatserRecord extends HandShakeRecord{

	private byte[] encryptedPreMaster = null;
	
	public RSAEncryptedPreMatserRecord(byte[] ePreMaster){
		encryptedPreMaster = ePreMaster;
	}
	
	@Override
	protected byte getHandShakeType() {
		return TalsConstance.HAND_SHAKE_TYPE_CLIENT_PRE_MASTER;
	}

	@Override
	protected int getContentLengthWithoutHandShakeTypeSize() {
		return encryptedPreMaster.length + 4;
	}

	@Override
	protected byte[] resolveSubStructWithoutHandShakeType() {
		
		byte[] len = Util.transIntegerToByteArray( encryptedPreMaster.length );
		byte[] buffer = new byte[len.length + encryptedPreMaster.length];
		
		int offset = 0; 
		for(int i=0; i<len.length; i++){
			buffer[offset++] = len[i];
		}
		
		for(int i=0; i<encryptedPreMaster.length; i++){
			buffer[offset++] = encryptedPreMaster[i];
		}
		
		return buffer;
	}

}
